package me.luger.project.picc.service;

import me.luger.core.dao.cache.redis.springdata.RedisManager;
import me.luger.core.dao.nutz.service.BaseService;
import me.luger.project.picc.entity.DrawList;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

@Repository
public class DrawListService extends BaseService<DrawList> {
    public DrawListService(Dao dao) {
        super(dao);
    }

    @Autowired
    RedisManager redisManager;

    public List<DrawList> doDraw(int rank, int count,int type){
        List<DrawList> drawListList = null;
        Sql sql = null;
        if(hasDoDraw(rank,type)){
            sql = Sqls.create("select * from drawList where doPrizeStatus = @rank and type = @type");
            sql.setParam("rank",rank+2);
            sql.setParam("type",type);
        }else{
            sql = Sqls.create("select * from drawList where doPrizeStatus = 1 and type = @type order by rand() limit @count");
            sql.setParam("count",count);
            sql.setParam("type",type);

        }

        drawListList = this.list(sql);
        if(null == drawListList || drawListList.size() == 0){
            return null;
        }

        if(!hasDoDraw(rank,type)){
            for(DrawList drawList : drawListList){
                drawList.setDoPrizeStatus(rank + 2);
                this.updateIgnoreNull(drawList);
            }
            setDoDrawStatus(rank,type);
        }

        return drawListList;
    }

    public String export2Excel(int type){
        Map<Integer,String> rankNameMap = new HashedMap();
        rankNameMap.put(0,"特等奖");
        rankNameMap.put(1,"一等奖");
        rankNameMap.put(2,"二等奖");
        rankNameMap.put(3,"三等奖");
        rankNameMap.put(4,"四等奖");
        try{
            Sql sql = Sqls.create("select * from drawList where doPrizeStatus > 1 and type = @type order by doPrizeStatus asc");
            sql.setParam("type",type);
            List<DrawList> drawListList = this.list(sql);
            if(null == drawListList || drawListList.size() == 0){
                return "";
            }

            if(hasDoDraw(0,type)&&hasDoDraw(1,type)&&hasDoDraw(2,type)&&hasDoDraw(3,type)){
                String fileName = "";
                if(type == 1){
                    fileName = "人民健康一卡通就医服务卡新客户"+"_"+System.currentTimeMillis()+".xls";
                }else if(type == 2){
                    fileName = "PICC人民健康APP注册"+"_"+System.currentTimeMillis()+".xls";
                }
                String filePath ="/usr/local/nginx/html/file/" + fileName;
                String fileHttpPath ="http://wx.picchealth.com/custom/file/" + fileName;
                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet("sheet1");

                Row row = sheet.createRow(0);

                Cell cell = row.createCell(0);
                cell.setCellValue("序号");
                cell = row.createCell(1);
                cell.setCellValue("奖项");
                cell = row.createCell(2);
                cell.setCellValue("姓名");
                cell = row.createCell(3);
                cell.setCellValue("手机号");


                int rowNum=1;
                for(DrawList item : drawListList){
                    row = sheet.createRow(rowNum);

                    cell = row.createCell(0);
                    cell.setCellValue(rowNum);
                    cell = row.createCell(1);
                    cell.setCellValue(rankNameMap.get(item.getDoPrizeStatus()-2));
                    cell = row.createCell(2);
                    cell.setCellValue(item.getName());
                    cell = row.createCell(3);
                    cell.setCellValue(item.getPhone());
                    rowNum++;
                }
                FileOutputStream fos = new FileOutputStream(filePath);
                wb.write(fos);
                if(null != fos){
                    fos.close();
                }
                return fileHttpPath;
            }

        }catch (Exception e){

        }
        return "";
    }

    public void setDoDrawStatus(int rank,int type){
        redisManager.set("drawList:doDraw:"+type+":"+rank,"1");
    }

    public boolean hasDoDraw(int rank,int type){
        String ret = redisManager.get("drawList:doDraw:"+type+":"+rank);
        if(null == ret){
            return false;
        }
        return true;
    }

    public void resetDoPrizeStatus(){
        this.execute(Sqls.create("update drawList set doPrizeStatus = 1"));
        redisManager.del("drawList:doDraw:1:0");
        redisManager.del("drawList:doDraw:1:1");
        redisManager.del("drawList:doDraw:1:2");
        redisManager.del("drawList:doDraw:1:3");
        redisManager.del("drawList:doDraw:1:4");

        redisManager.del("drawList:doDraw:2:0");
        redisManager.del("drawList:doDraw:2:1");
        redisManager.del("drawList:doDraw:2:2");
        redisManager.del("drawList:doDraw:2:3");
        redisManager.del("drawList:doDraw:2:4");
    }
}
